package com.kaver.sso.web.aop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kaver.sso.def.BizException;
import com.kaver.sso.def.Result;

/**
 * 全局的的异常拦截器（拦截所有的控制器）（带有@RequestMapping注解的方法上都会拦截）
 *
 * @author guopeng
 */
@ControllerAdvice(basePackages="com.kaver.sso.web.controller.rest")
@Order(-1)
public class RestGlobalExceptionHandler {

	private final static Logger logger = LoggerFactory.getLogger(RestGlobalExceptionHandler.class);
	/**
	 * 拦截业务异常
	 */
	@ExceptionHandler(BizException.class)
	@ResponseBody
	public <T> Result<T> exc(BizException e) {
		return Result.createError(e);
	}

	/**
	 * 拦截系统异常
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public <T> Result<T> exc(Exception e) {
		logger.error("Exception error", e);
		return Result.createSysError();
	}
}